﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonaLibrary.Data;
using PersonaLibrary.View;

namespace PersonaLibrary.Commands.EditCommands
{
    public class SymplyDeleteCommand<T> : CommandBase
        where T : BaseEntity
    {
        public override bool CanExecute(object parameter)
        {
            return parameter is int;
        }

        public override void Execute(object parameter)
        {
            DeleteView deleteView = new DeleteView();
            if (deleteView.ShowDialog().HasValue && deleteView.DialogResult.Value)
            {
                using (LinqToDbDataContext context = new LinqToDbDataContext())
                {
                    context.GetTable<T>().DeleteOnSubmit(context.GetTable<T>().Single(x => ((T) x).Id == (int) parameter));
                    context.SubmitChanges();
                }
            }
        }
    }
}
